package model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.dto.Goal;
import utility.Database;
import utility.Log;

public class GoalModel extends Database {
	public boolean insert(Goal dto) {
		String sql = "insert into tbl_goal(goal_id,goal_minute,scorer_id,assister_id,own_goal,match_id) values(null,?,?,?,?,?)";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, dto.getMinute());
			pstmt.setInt(2, dto.getScorerId());
			pstmt.setInt(3, dto.getAssisterId());
			pstmt.setInt(4, dto.isOwnGoal()?1:0);
			pstmt.setInt(5, dto.getMatchId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}

		return false;
	}

	public boolean delete(int goal_id) {
		String sql = "delete from tbl_goal where goal_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, goal_id);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public boolean update(Goal dto) {
		String sql = "update tbl_goal set goal_minute=?,scorer_id=?,assister_id=?,own_goal=? where goal_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, dto.getMinute());
			pstmt.setInt(2, dto.getScorerId());
			pstmt.setInt(3, dto.getAssisterId());
			pstmt.setInt(4, dto.isOwnGoal()?1:0);
			pstmt.setInt(5, dto.getId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public Goal get(int goal_id) {
		String sql = "select * from tbl_goal where goal_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, goal_id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				Goal dto = new Goal();
				dto.setId(goal_id);
				dto.setScorerId(rs.getInt("scorer_id"));
				dto.setMinute(rs.getInt("goal_minute"));
				dto.setAssisterId(rs.getInt("assister_id"));
				dto.setOwnGoal((rs.getInt("own_goal")==1)?true:false);
				return dto;
			}
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(String search, int limit, int page) {
		String sql = "select * from tbl_country where country_name like ? ORDER BY country_id DESC limit ?,?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			pstmt.setInt(2, (page - 1) * limit);
			pstmt.setInt(3, limit);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(int limit, int page) {
		return list("", limit, page);
	}

	public int count(String search) {
		String sql = "select count(*) from tbl_country where country_name like ?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			ResultSet rs = pstmt.executeQuery();
			return rs.next() ? rs.getInt(1) : 0;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return 0;
	}

	public int count() {
		return count("");
	}

}
